import React, { useState } from "react";
import { Input, message, Modal } from "antd";
import { putAxios } from "@/service/axiosManager";

export default function ShowDialog({ open, setOpen }: { open: boolean; setOpen: (open: boolean) => void }) {
    const [newPassword, setNewPassword] = useState('');
    const [confirmPassword, setConfirmPassword] = useState('');
    const [oldPassword, setOldPassword] = useState('');
    const [messageApi, contextHolder] = message.useMessage();

    return (
        <>
            <Modal
                title="修改密码"
                centered
                open={open}
                onOk={() => {
                    console.log('新密码:', newPassword);
                    console.log('确认密码:', confirmPassword);
                    console.log('旧密码:', oldPassword);
                    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
                    putAxios('/sys/user/updatePassword', {
                        username: userInfo.username,
                        password: newPassword,
                        confirmpassword: confirmPassword,
                        oldpassword: oldPassword
                    }).then((res) => {
                        console.log(res);
                        if (res.code == 200) {
                            message.success('修改成功');
                            setOpen(false);
                        } else {
                            message.error(res.message);
                        }
                    });
                }}
                okText="确定"
                cancelText="取消"
                onCancel={() => setOpen(false)}
                width={500}
            >
                <Input.Password
                    className="mb-2 mt-2"
                    placeholder="输入新密码"
                    value={newPassword}
                    onChange={(e) => setNewPassword(e.target.value)}
                />
                <Input.Password
                    className="mb-2"
                    placeholder="再次输入新密码"
                    value={confirmPassword}
                    onChange={(e) => setConfirmPassword(e.target.value)}
                />
                <Input.Password
                    className="mb-2"
                    placeholder="输入旧密码"
                    value={oldPassword}
                    onChange={(e) => setOldPassword(e.target.value)}
                />
            </Modal>
        </>
    );
}
